<?php

/**
 * Description of cart
 * 
 * @author 叶风
 * Qq:    355007778
 * Email: ye.fn@163.com
 */

namespace c\shop;
\YF_f::initC('\c\auth\permission')->login();
\lang::load('shop');

class cart extends \control
{
    var $table = 'shop_cart';
	
    function index_home()
    {
        $t = array('t1'=>array('as'=>$this->table),
                   'd1'=>array('as'=>'goods', 'join'=>'LEFT JOIN', 'on'=>'d1.id=t1.data_id'));
        $res = $this->db->get($t, 't1.id,t1.name,t1.num,t1.money,t1.data_id,t1.pic,d1.num as store_num,d1.state', 't1.user_id='.\YF_f::$login_user_id);
        if (!$res)
            $this->clue(\lang::g('cart is empty'), ROOT_URL);
        $this->loadTemplate('shop/cart/home', array('res'=>$res));
    }
    function index_add()
    {
        $g = $_GET;
        $gID = intval($g['data_id']);
        $num = intval($g['num']);
        $num<1 && $num=1;
        
        $r = $this->db->getOne('goods', 'title,money', 'id='. $gID);
        $data = array('user_id'=>\YF_f::$login_user_id, 'num'=>$num, 'data_id'=>$gID, 'pic'=>$g['pic'],
                'name'=>$r['title'], 'money'=>$num * $r['money']);
        $this->db->save($this->table, $data, 'REPLACE');
        
        if (isset($g['next']))
            \url::jump('?u=cshop\cart-fsum&id='. $this->db->insert_id());

        ajax_json(TRUE);
    }
    function index_del()
    {
        $id = intval($_GET['id']);
        $this->db->del($this->table, 'id='.$id. ' AND user_id='.\YF_f::$login_user_id);
        ajax_json(TRUE);
    }
    
    function index_sum()
    {
        $ids = intval_ids($_GET['id']);
        $t = array('t1'=>array('as'=>$this->table),
                   'd1'=>array('as'=>'goods', 'join'=>'LEFT JOIN', 'on'=>'d1.id=t1.data_id'));
        $res = $this->db->get($t, 't1.*',  't1.id IN('. $ids .') AND t1.num<=d1.num AND d1.state>0');

        $conf = include CONFIG_DIR .'shop.php';
        $this->loadTemplate('shop/cart/sum', array('res'=>$res, 'expre'=>$conf['expre']));
    }
    
    // 确认下单
    function index_order()
    {
        $p = $_POST;
        if (!$p['c_ids'])
           $this->clue(\lang::g ('Over')); 
        
        
        //order       
        $luserID = \YF_f::$login_user_id;
        $time = date('YmdHis');
        $sn =  $time . sprintf('%04d', rand(10, 9999));
        $data = array('sn'=>$sn,
                    'user_id'=> $luserID,
                    'state'=>1,
                    'note'=>$p['note'],
                    'time'=> $time
                    );
        
        //get addre
        $addre_id = intval($p['u_address']);
        if ($addre_id>0)
         {
            $addreD = $this->db->getOne('user_address', '*', 'id='. $addre_id . ' AND user_id='.$luserID);
            $data['to_address'] = $addreD['area']. $addreD['street'] . ','. $addreD['zipcode'];
            $data['to_compellation'] = $addreD['compellation'];
            $data['to_tel'] = $addreD['tel'];
         }
         
        //save
        $b = $this->db->save('shop_order', $data);
        if (!$b)
            $this->clue(\lang::g ('save fail'));
        $id = $this->db->insert_id();
        
        //detail
        $c_ids = intval_ids($p['c_ids']);
        $sql = 'INSERT INTO '. $this->db->pre .'shop_detail (order_id,goods_id, name, num, money, pic)'
                . " SELECT $id, g.id, t1.name, t1.num, t1.money, t1.pic"
                . ' FROM '. $this->db->pre . $this->table .' t1'
                . " JOIN {$this->db->pre}goods g ON(g.id=t1.data_id)"
                . " WHERE t1.id IN($c_ids)". ' AND g.state>0 AND g.num>=t1.num';
        $b = $this->db->query($sql);
        if ($this->db->num_affected()<1)
         {
            $this->db->del('shop_order', 'id='.$id);
            $this->clue(\lang::g ('part save fail'));
         }
		// goods -1
        $a = $this->db->getCol('shop_detail', 'goods_id', 'order_id='.$id);
		$this->db->upField('goods', 'num', implode(',', $a), -1);
		
        //flow
        \YF_f::initC('\c\shop\order\g')->flow($id, \lang::g('Submit order.'));
         
        //计算总额
        $sm = $this->db->getOneField('shop_detail', 'sum(money)', 'order_id='.$id);

        $conf = include CONFIG_DIR .'shop.php';
        $expre = $conf['expre'];
        $fee = $sm<$expre['free_money'] ? $expre['fee'] : 0; 
        $sm +=  $fee;

        
        $this->db->save('shop_order', array('money'=>$sm, 'pay_money'=>$sm, 'expre_fee'=>$fee), 'id='. $id);
        
        //end
        $this->db->del($this->table, "id IN($c_ids) AND user_id=". $luserID);
        \log::write('shop_order', 'add', $id);
        \url::jump('?u=cshop\pay&id='. $id);
    }
}
